﻿using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


namespace DAL
{
    public class ChiTietHoaDonBanHangDAL
    {
        private SqlConnection cn = null;
        public ChiTietHoaDonBanHangDAL()
        {
            string stringConnection = ConfigurationSettings.AppSettings["connectionstring"].ToString();
            cn = new SqlConnection(stringConnection);
            cn.Open();
        }

        public void INSERT(ChiTietHoaDonBanHang D)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_CHITIETHOADONBANHANG_Insert";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaHoaDonBanHang", D.MaHoaDonBanHang);
            cm.Parameters.AddWithValue("@MaSanPham", D.MaSanPham);
            cm.Parameters.AddWithValue("@SoLuong", D.SoLuong);
            cm.Parameters.AddWithValue("@Gia", D.Gia);
            cm.Parameters.AddWithValue("@TongTien", D.TongTien);
            cm.ExecuteNonQuery();
        }

        public void UPDATE(ChiTietHoaDonBanHang D)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_CHITIETHOADONBANHANG_Update";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;

            cm.Parameters.AddWithValue("@MaHoaDonBanHang", D.MaHoaDonBanHang);
            cm.Parameters.AddWithValue("@MaSanPham", D.MaSanPham);
            cm.Parameters.AddWithValue("@SoLuong", D.SoLuong);
            cm.Parameters.AddWithValue("@Gia", D.Gia);
            cm.Parameters.AddWithValue("@TongTien", D.TongTien);
            cm.ExecuteNonQuery();
        }

        public void DELETE(int iD)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_CHITIETHOADONBANHANG_Delete";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaChiTietHoaDonBanHang", iD);
            cm.ExecuteNonQuery();
        }

        public ChiTietHoaDonBanHangCollection GET(string Where, int PageIndex, int PageSize, string OrderBy, string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand();
            cm.CommandText = "sp_CHITIETHOADONBANHANG_Get";
            cm.Connection = this.cn;
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            SqlParameter param = new SqlParameter("@TotalRecords", DbType.Int32);
            param.Direction = ParameterDirection.Output;
            cm.Parameters.Add(param);
            SqlDataAdapter da = new SqlDataAdapter(cm);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ChiTietHoaDonBanHangCollection ChiTietHoaDonBanHangCollection = new ChiTietHoaDonBanHangCollection();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                ChiTietHoaDonBanHang D = new ChiTietHoaDonBanHang();
                D.MaChiTietHoaDonBanHang = int.Parse(ds.Tables[0].Rows[i]["MaChiTietHoaDonBanHang"].ToString());
                D.MaHoaDonBanHang = int.Parse(ds.Tables[0].Rows[i]["MaHoaDonBanHang"].ToString());
                D.MaSanPham = int.Parse(ds.Tables[0].Rows[i]["MaSanPham"].ToString());
                D.SoLuong = int.Parse(ds.Tables[0].Rows[i]["SoLuong"].ToString());
                D.Gia = int.Parse(ds.Tables[0].Rows[i]["Gia"].ToString());
                D.TongTien = int.Parse(ds.Tables[0].Rows[i]["TongTien"].ToString());
                ChiTietHoaDonBanHangCollection.Add(D);
            }
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ChiTietHoaDonBanHangCollection;
        }
    }
}
